var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //门店id
        brandId: 0,
        //商品条数
        Count: "",
        GoodsList: [
            {
              commodityId: 1,
              commodityName: '商品1',
              productNumber: 0,
              commodityPrice: 10,
              commodityDetails: '商品1详情',
              replenishCount: 1, // 新增的属性，用于保存加减按钮的数字
              selected: false
            },
            {
              commodityId: 2,
              commodityName: '商品2',
              productNumber: 0,
              commodityPrice: 20,
              commodityDetails: '商品2详情',
              replenishCount: 1, // 新增的属性，用于保存加减按钮的数字
              selected: false
            },
            {
                commodityId: 3,
                commodityName: '商品3',
                productNumber: 0,
                commodityPrice: 20,
                commodityDetails: '商品3详情',
                replenishCount: 1, // 新增的属性，用于保存加减按钮的数字
                selected: false
              },
          ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //当前管理员的门店ID
        this.setData({
            brandId: app.globalData.bread.brandId
        })

        this.getGoodsList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    //查询当前门店的所有产品(套餐)
    getGoodsList: function() {
        let that = this;

        util.request(api.GoodsList, {
            brandId: that.data.brandId
          }).then(function(res) {
              that.setData({
                  GoodsList: res.data.data,
                  Count: res.data.count,
              })
          })
    },

    //补货
    onMinus: function(e) {
        const index = e.currentTarget.dataset.index;
        if (this.data.GoodsList[index].replenishCount > 0) {
          this.setData({
            [`GoodsList[${index}].replenishCount`]: this.data.GoodsList[index].replenishCount - 1
          });
        }
      },
      onPlus: function(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
          [`GoodsList[${index}].replenishCount`]: this.data.GoodsList[index].replenishCount + 1
        });
      },
      onInputChange: function(e) {
        const index = e.currentTarget.dataset.index;
        const value = parseInt(e.detail.value);
        if (!isNaN(value) && value >= 0) {
          this.setData({
            [`GoodsList[${index}].replenishCount`]: value
          });
        }
      },
      onReplenish: function(e) {
        const index = e.currentTarget.dataset.index;
        let commodityId = this.data.GoodsList[index].id;
        let count = this.data.GoodsList[index].replenishCount;
        let commodityName = this.data.GoodsList[index].commodityName;
        // 补货按钮的逻辑处理，发送请求给后台进行补货操作
        let that = this;
        wx.showModal({
            title: commodityName + '补货' + count + '个',
            content: '确认给' + commodityName + '补货' + count + '个吗?',
            success(res) {
              if (res.confirm) {
                util.request(api.GoodsAdd, {
                    commodityId: commodityId,
                    count: count
                  }).then(function(res) {
                      //刷新页面
                      wx.redirectTo({
                        url: '/pages/admin/goods/goods', // 当前页面的路径
                        success: function() {
                            wx.showToast({
                                title: '补货成功',
                                icon: 'none',
                                duration: 2000
                              });
                        },
                      });
                  })
              } else if (res.cancel) {
                // 用户点击了取消按钮
                wx.showToast({
                    title: '补货失败',
                    icon: 'none',
                    duration: 2000
                  });
              }
            }
        })
      }

})